package cn.e4j.modular.crm.service.impl;

import cn.e4j.core.dto.Dtos;
import cn.e4j.core.dto.impl.Dto;
import cn.e4j.core.util.DateUtils;
import cn.e4j.modular.crm.entity.Gwai760;
import cn.e4j.modular.crm.mapper.Gwai780Mapper;
import com.baomidou.dynamic.datasource.annotation.DS;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Map;

/**
 * <p>
 * 服务实现类
 * </p>
 *
 * @author shixc
 * @since 2019-02-02
 */
@Service
public class Gwai780ServiceImpl extends ServiceImpl<Gwai780Mapper, Gwai760> {

    @Transactional(readOnly = true)
    public List<Map<String, Object>> getList(Page<Object> page, Dto dto) {
        List<Map<String, Object>> gwdaDtos = new ArrayList<>();
        if (!dto.getString("COMPANY").equals("") && !dto.getString("XX003_Y").equals("") && !dto.getString("XX003_M").equals("") && !dto.getString("XX001").equals("")) {
            dto.put("XX003", dto.getString("XX003_Y") + dto.getString("XX003_M"));
            gwdaDtos = this.baseMapper.listPage(page, dto);
        }
        return gwdaDtos;
    }

    @Transactional
    public void check(Dto inDto, Dto info) {
        //ShiroUser user = ShiroKit.getUser();
        inDto.put("UDF01", "Y");
        inDto.put("UDF02", DateUtils.formatDate(new Date(), "yyyyMMdd"));
        inDto.put("UDF03", info == null ? null : info.getString("BIZ_CODE"));
        this.baseMapper.check(inDto);
    }

    @Transactional(readOnly = true)
    public List<Dto> getDeatil(Dto dto) {
        Dto money = this.baseMapper.getMoney(dto);
        BigDecimal start1 = money.getBigDecimal("QC0211");
        BigDecimal start2 = money.getBigDecimal("QC0213");
        BigDecimal start3 = money.getBigDecimal("QC0212");
        List<Dto> list = this.baseMapper.getDeatil(dto);
        List<Dto> result = new ArrayList<>();
        List<Dto> result1 = new ArrayList<>();
        List<Dto> result2 = new ArrayList<>();
        List<Dto> result3 = new ArrayList<>();
        String UDF12 = "";
        for (int i = 0; i < list.size(); i++) {
            if (list.get(i).getString("UDF12").indexOf("帐期") != -1) {
                result1.add(list.get(i));
            } else if (list.get(i).getString("UDF12").indexOf("现款") != -1) {
                result2.add(list.get(i));
            } else if (list.get(i).getString("UDF12").indexOf("铺货") != -1) {
                result3.add(list.get(i));
            }
        }
        Dto inDto = Dtos.newDto();
        List<Dto> list1 = new ArrayList<>();
        BigDecimal count1 = new BigDecimal(0);
        BigDecimal count2 = new BigDecimal(0);
        for (int j = 0; j < result1.size(); j++) {
            list1.add(result1.get(j));
            count1 = count1.add(result1.get(j).getBigDecimal("XH"));
            count2 = count2.add(result1.get(j).getBigDecimal("SK"));
            if (j == result1.size() - 1) {
                if (list1.size() > 0) {
                    inDto.put("TG003", "账期期初");
                    inDto.put("TG002", "");
                    inDto.put("XH", "");
                    inDto.put("SK", start1);
                    inDto.put("UDF12", "");
                    inDto.put("REMARK", "");
                    result.add(inDto);
                    result.addAll(list1);
                    Dto pDto = Dtos.newDto();
                    pDto.put("TG003", "账期小计");
                    pDto.put("TG002", "");
                    pDto.put("XH", count1.setScale(2));
                    pDto.put("SK", count2.setScale(2));
                    pDto.put("UDF12", "");
                    pDto.put("REMARK", "");
                    result.add(pDto);
                    pDto = Dtos.newDto();
                    pDto.put("TG003", "账期期末余额");
                    pDto.put("TG002", "");
                    pDto.put("XH", "");
                    pDto.put("SK", start1.add(count1).subtract(count2).setScale(2));
                    pDto.put("UDF12", "");
                    pDto.put("REMARK", "");
                    result.add(pDto);
                }
            }
        }
        List<Dto> list2 = new ArrayList<>();
        count1 = new BigDecimal(0);
        count2 = new BigDecimal(0);
        for (int j = 0; j < result2.size(); j++) {
            list2.add(result2.get(j));
            count1 = count1.add(result2.get(j).getBigDecimal("XH"));
            count2 = count2.add(result2.get(j).getBigDecimal("SK"));
            if (j == result2.size() - 1) {
                if (list2.size() > 0) {
                    inDto = Dtos.newDto();
                    inDto.put("TG003", "现款期初");
                    inDto.put("TG002", "");
                    inDto.put("XH", "");
                    inDto.put("SK", start2);
                    inDto.put("UDF12", "");
                    inDto.put("REMARK", "");
                    result.add(inDto);
                    result.addAll(list2);
                    Dto pDto = Dtos.newDto();
                    pDto.put("TG003", "现款小计");
                    pDto.put("TG002", "");
                    pDto.put("XH", count1.setScale(2));
                    pDto.put("SK", count2.setScale(2));
                    pDto.put("UDF12", "");
                    pDto.put("REMARK", "");
                    result.add(pDto);
                    pDto = Dtos.newDto();
                    pDto.put("TG003", "现款期末余额");
                    pDto.put("TG002", "");
                    pDto.put("XH", "");
                    pDto.put("SK", start2.add(count1).subtract(count2).setScale(2));
                    pDto.put("UDF12", "");
                    pDto.put("REMARK", "");
                    result.add(pDto);
                }
            }
        }
        List<Dto> list3 = new ArrayList<>();
        count1 = new BigDecimal(0);
        count2 = new BigDecimal(0);
        for (int j = 0; j < result3.size(); j++) {
            if ("铺货".equals(result2.get(j).getString("UDF12"))) {
                list3.add(result2.get(j));
                count1 = count1.add(result2.get(j).getBigDecimal("XH"));
                count2 = count2.add(result2.get(j).getBigDecimal("SK"));
            }
            if (j == result3.size() - 1) {
                if (list3.size() > 0) {
                    inDto.put("TG003", "铺货期初");
                    inDto.put("TG002", "");
                    inDto.put("XH", "");
                    inDto.put("SK", start3);
                    inDto.put("UDF12", "");
                    inDto.put("REMARK", "");
                    result.add(inDto);
                    result.addAll(list3);
                    Dto pDto = Dtos.newDto();
                    pDto.put("TG003", "铺货小计");
                    pDto.put("TG002", "");
                    pDto.put("XH", count1.setScale(2));
                    pDto.put("SK", count2.setScale(2));
                    pDto.put("UDF12", "");
                    pDto.put("REMARK", "");
                    result.add(pDto);
                    pDto = Dtos.newDto();
                    pDto.put("TG003", "铺货期末余额");
                    pDto.put("TG002", "");
                    pDto.put("XH", "");
                    pDto.put("SK", start3.add(count1).subtract(count2).setScale(2));
                    pDto.put("UDF12", "");
                    pDto.put("REMARK", "");
                    result.add(pDto);
                }
            }
        }
        return result;
    }

    @Transactional(readOnly = true)
    public Dto getUser(String ID) {
        Dto user = this.baseMapper.getUser(ID);
        return user;
    }
}
